package com.gams.springboot.services.impl;

import com.gams.springboot.dao.RoleDao;
import com.gams.springboot.pojo.Role;
import com.gams.springboot.pojo.User;
import com.gams.springboot.services.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author tql
 * @current springboot
 * @create 2021-09-9-10:14
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> selectAllRole() {
        return roleDao.SelectAllRole();
    }

    @Override
    public PageInfo findAllUser(int page, int limit) {
        //开启分页
        PageHelper.startPage(page,limit);
        //获取用户列表
        List<Role> role = roleDao.SelectAllRole();
        //加载到分页信息类里面
        PageInfo<Role>  pageInfo = new PageInfo(role);
        return pageInfo;
    }

    @Override
    public long addRole(Role role,List<Long> list) {
        long result = 0;
        if(role.getCreateTime()==null){
            role.setCreateTime(new Date());
        }

        if(roleDao.addRole(role)>0){
            long id = roleDao.queryId(role.getName());
            for(Long s : list){
                result = roleDao.insertMenuRole(s,id);
            }
        }
        return result;
    }

    @Override
    public int updateRole(Role role,String id,List<Long> list) {
        int result = 0;
        if(role.getUpdateTime()==null){
            role.setCreateTime(new Date());
        }
        long roleId = Integer.parseInt(id);
        if(roleDao.updateRole(role)>0){
            if(roleDao.deleteRoleMenu(roleId)>0)
                for(Long s : list){
                    result = roleDao.insertMenuRole(s,roleId);
                }
        }
        return result;
    }

    @Override
    public int deleteRole(long id) {
        return roleDao.deleteRole(id);
    }

    @Override
    public long deleteRoleMenu(long id) {
        return roleDao.deleteRoleMenu(id);
    }

    @Override
    public int queryId(String name) {
        return roleDao.queryId(name);
    }

    @Override
    public int insertMenuRole(long menuId, long roleId) {
        return roleDao.insertMenuRole(menuId, roleId);
    }

    @Override
    public List<Role> selectRoleIdList(long userId) {
        return roleDao.selectRoleIdList(userId);
    }

    @Override
    public List<Role> selectAllRoleByUserId(long userId) {
        return roleDao.selectAllRoleByUserId(userId);
    }

}
